const questionService = require('../service/question.service')
const R = require('../utils/R')
const { QUESTION_EXCEPTION } = require('../error/errorType')

class QuestionController {
  async question (ctx, next) {
    try {
      // 具体代码
      const { typeId } = ctx.questionInfo
      // console.log(typeId);
      const result = await questionService.question()

      // console.log(result);
      //当输入typeId 2的时候，就返回2相关的信息，=》只能输 1-6  =》 其他数字返回空数组
      if (typeId) {
        let arr = []
        result.forEach(element => {
          if (element.typeId == typeId) {
            arr.push(element)
          }
        });
        // console.log(arr);
        ctx.dto = R.ok('常见问题成功', arr)

      }
      else {
        ctx.dto = R.ok('常见问题成功', result)
      }

    } catch (err) {
      console.log(err.message)
      return ctx.app.emit('error', new Error(QUESTION_EXCEPTION), ctx)
    }
    await next()
  }
}

module.exports = new QuestionController()